<!--
  @Descripttion:用户中心-项目报名
  @Author：yejianbo
  @Date：2022年05月26日 12:35:40
-->
<template>
    <section class="page">
        <pageCom>
            <div class="content-page">
                <div class="content">
                <el-breadcrumb
                    separator-class="el-icon-arrow-right"
                    style="margin-bottom: 20px"
                >
                    <el-breadcrumb-item>首页</el-breadcrumb-item>
                    <el-breadcrumb-item :to="{ name: 'userCenter' }"
                    >用户中心</el-breadcrumb-item
                    >
                    <el-breadcrumb-item>
                    <span style="color: #3d6cb0">报名项目</span>
                    </el-breadcrumb-item>
                </el-breadcrumb>
                <p class="page-title">报名项目</p>
                <div class="page-content">
                    <el-table
                    :data="tableData"
                    >
                        <el-table-column
                        prop="title"
                        label="项目名称"
                        width="200">
                        <template slot-scope="scope">
                        <p class="product-title-link" @click="linkToProductDetail(scope.row.projectId)">{{ scope.row.title }}</p>
                        </template>
                        </el-table-column>
                        <el-table-column
                            prop="entryType"
                            label="报名类型"
                            align="left">
                        <template slot-scope="scope">
                            <!-- 报名类型;1:团队;2:个人 -->
                            <p>{{ Number(scope.row.entryType) === 1 ? '团队': '个人' }}</p>
                        </template>
                        </el-table-column>
                        <el-table-column align="left" label="报名资料">
                            <template slot-scope="scope">
                            <!-- 报名类型;1:团队;2:个人 -->
                            <p>{{ scope.row.annexUrl && scope.row.annexName ? '已上传': '未上传' }}</p>
                        </template>
                        </el-table-column>
                        <el-table-column
                        prop="province"
                        align="left"
                        label="报名状态"
                        >
                        <template slot-scope="scope">
                            <!-- 报名(参赛)状态;1:未报名;2:报名中 -->
                            <p>{{ scope.row.entryStatusStr}}</p>
                        </template>
                        </el-table-column>
                        <el-table-column
                        prop="province"
                        align="left"
                        label="遴选阶段"
                        >
                        <template slot-scope="scope">
                            <p>{{ scope.row.checkStatusStr}}</p>
                        </template>
                        </el-table-column>
                        <el-table-column
                        prop="rateScore"
                        align="left"
                        label="评分"
                        >
                        <template slot-scope="scope">
                            <p>{{ scope.row.rateScore || '--'}}</p>
                        </template>
                        </el-table-column>
                        <el-table-column
                        prop="entryTime"
                        align="center"
                        label="报名时间"
                        width="110px">
                        </el-table-column>
                        <el-table-column
                        prop="endTime"
                        align="center"
                        label="结束时间"
                        width="110px">
                        </el-table-column>
                        <el-table-column
                        prop="closingDate"
                        align="left"
                        label="截止上传时间"
                        width="110px">
                        <template slot-scope="scope">
                            {{ scope.row.closingDate || '--' }}
                        </template>
                        </el-table-column>
                        <el-table-column
                        align="center"
                        prop="zip"
                        label="操作"
                        width="240"
                        fixed="right"
                        >
                        <template slot-scope="scope">
                            <div style="display: flex;flex-flow: row wrap;justify-content: flex-end;">
                                <FileUpload v-if="Number(scope.row.jobType) === 1 && scope.row.isShowUploadBtn" style="margin: 0 10px"  :fileType="['zip']"  @success="handleAvatarSuccess">
                                    <el-button style="margin: 0 10px" type="text" @click="uploadFile(scope.row)">上传</el-button>
                                </FileUpload>
                                <el-button type="text" style="margin: 0 10px" @click="linkToCheckProductDetail(scope.row.id)">查看报名</el-button>
                                <el-popconfirm
                                title="确定取消报名吗？"
                                @confirm="deleteItem(scope.row)"
                                >
                                    <el-button v-if="scope.row.canCancel"  slot="reference" type="text" style="color: #FA606A;margin: 0 10px">取消报名</el-button>
                                </el-popconfirm>
                                <el-button v-if="scope.row.resultId !== null && scope.row.resultId > 0" type="text" style="margin: 0 10px" @click="linkToCheckResultDetail(scope.row.resultId)">查看公示</el-button>
                            </div>
                        </template>
                        </el-table-column>
                    </el-table>
                    <div style="display: flex;flex-flow: row nowrap; justify-content: center;padding: 46px 0;">
                        <el-pagination
                            class="bottom-page"
                            background
                            layout="prev, pager, next"
                            :total="total"
                            :page-size="pageSize"
                            :page-sizes="[6, 12, 24, 36, 48]"
                            @size-change="pagesizeHandle"
                            @current-change="currentChange"
                            :current-page="pageNo"
                            >
                            </el-pagination>
                        </div>
                    </div>
                </div>
            </div>
        </pageCom>
        
    </section>
  </template>
  
  <script>
  import pageCom from "@/components/page/index.vue";
  import { getStudentEntryPage, uploadAnnex, deleteAnnex } from "../../request/api";
  import moment from 'moment'
  import FileUpload from '@/components/FileUpload'
  export default {
    // 组件名称
    name: "registrationProduct",
    // 组件参数 接收来自父组件的数据
    props: {},
    // 局部注册的组件
    components: { pageCom, FileUpload },
    computed: {
    },
    created() {
        this.getData()
    },
    watch: {
    },
    // 组件状态值
    data() {
      return {
        action:
        process.env.VUE_APP_BASE_API + "/app-api/student/web-student/upload",
        tableData: [],
        total: 0,
        pageNo: 1,
        pageSize: 10,
        currentItem: ''
      };
    },
    // 组件方法
    methods: {
        linkToProductDetail(id) {
            this.$router.push({name: 'productDetail', query: {id: id}})
        },
        linkToCheckProductDetail(id) {
            this.$router.push({name: 'checkProductDetail', query: {id: id}})
        },
        linkToCheckResultDetail(id) {
            this.$router.push({name: 'productResultDetail', query: {id: id}})
        },
        deleteItem(item){
            deleteAnnex({id: item.id}).then(res => {
                console.log('delete>>>', res)
                if (res && res.code === 0) {
                    this.$message.success('取消报名成功')
                    this.getData()
                } else {
                    this.$message.error(res.msg || '请稍后重试');
                }
            })
        },
        uploadFile(item) {
            this.currentItem = item
        },
        onError() {
            this.$message.warning("上传失败，请稍后重试");
        },
        beforeAvatarUpload(file) {
            let fileName = file.name.substring(file.name.lastIndexOf('.') + 1)
            let falg = fileName === 'zip'
            if (!falg) {
            this.$message('请上传zip格式文件~')
            }
            this.loading = falg;
            return falg;
        },
        handleAvatarSuccess({url, file}) {
            uploadAnnex({
                annexName: file.name,
                annexUrl: url,
                id: this.currentItem.id
            }).then(res => {
                if (res && res.code === 0) {
                    this.$message.success("上传成功");
                    this.getData()
                } else {
                    this.$message.error(res.msg || '上传失败，请稍后重试');
                }
            })
        },
        pagesizeHandle(val) {
          this.pageSize = Number(val);
          this.getData();
        },
        currentChange(e) {
            this.pageNo = e;
            this.getData();
        },
        getData() {
            getStudentEntryPage({
                pageNo: this.pageNo,
                pageSize: this.pageSize,
                scheduleType: 1
            }).then(res => {
                if(res && res.code === 0) {
                    const {list, total} = res.data
                    this.tableData = list.map(item => {
                        let newDate = moment()
                        let startDate = moment(item.startTime, 'YYYY-MM-DD HH:mm:ss')
                        let entryTime = moment(item.entryTime, 'YYYY-MM-DD HH:mm:ss') // 报名时间
                        let endTime = moment(item.endTime, 'YYYY-MM-DD HH:mm:ss')
                        let closeTime = moment(item.closingDate, 'YYYY-MM-DD HH:mm:ss')
                        item.entryStatusStr = newDate.isBefore(endTime) ? '已报名' : '已结束'
                        item.checkStatusStr = ''
                        item.isShowUploadBtn = true
                        item.canCancel = false
                        if (endTime.isBefore(newDate) && Number(item.checkStatus) !== 1) {
                            item.checkStatusStr = '未入选'
                        } else if (Number(item.checkStatus) === 1) {
                            item.checkStatusStr = '已入选'
                        } else {
                            item.checkStatusStr = '--'
                        }
                        if (newDate.isAfter(closeTime)) {
                            item.isShowUploadBtn = false
                        }
        
                        if(Number(item.scheduleType) === 1 && 
                            newDate.isBefore(endTime) &&
                            newDate.isAfter(startDate) && 
                            newDate.isAfter(entryTime)) {
                                item.canCancel = true
                        }
                        return item
                    })
                    this.total = total
                }
            })
        },
    },
  };
  </script>
  <style lang="less" scoped>
  
  .page {
    position: relative;
    width: 100%;
    height: 100%;
    min-width: 1000px;
  
    .content-page {
      position: relative;
      width: 100%;
      height: 100%;
      .names{
        font-size: 15px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #37383D;
        line-height: 21px;
        margin-bottom: 20px;
        display: flex;
        span{
          color: #9597A0;
          width: 78px;
          margin-right: 20px;
        }
      }
  
      .page-title {
        font-size: 25px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #37383D;
        line-height: 36px;
        margin: 30px 0;
      }
      .bannber {
        position: relative;
        display: block;
        width: 100%;
        margin-bottom: 25px;
        object-fit: cover;
        height: 170px;
      }
  
      .content {
        position: relative;
        width: 100%;
        max-width: 1200px;
        padding: 23px 30px;
        margin: 0 auto;
  
        .content-title {
          font-size: 18px;
          font-family: PingFangSC-Medium, PingFang SC;
          font-weight: 500;
          color: #37383D;
          line-height: 25px;
          margin-bottom: 16px;
          &::before {
              content: '*';
              color: #F56C6C;
              margin-right: 4px;
          }
        }
        /deep/.el-form-item__label{
          font-size: 16px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
        }
      }
    }
  }
  .product-title-link {
    font-size: 15px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #4677B9;
    line-height: 21px;
    cursor: pointer;
  }
  /deep/ .el-button--text {
    font-size: 15px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #4677B9;
    line-height: 21px;
  }
  /deep/ .el-breadcrumb__inner {
    font-size: 18px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #37383d;
    line-height: 28px;
    cursor: pointer;
  }
  .hover-icon {
    cursor: pointer;
  }
  .showAuthentication{
      position: fixed;
      z-index: 2014;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      background: rgba(0, 0, 0, 0.3);
    .showAuthenticationBox{
      width: 360px;
      height: 241px;
      padding: 66px 30px 90px;
      border-radius: 12px;
      background: #fff;
      position: relative;
      .content{
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 15px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #37383D;
        line-height: 21px;
      }
      .content2{
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 15px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #37383D;
        line-height: 21px;
      }
      .title{
        position: absolute;
        width: 100%;
        left: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 18px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #37383D;
        line-height: 25px;
        top:25px;
      }
      .title2{
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #37383D;
        line-height: 20px;
        margin-bottom: 12px;
      }
      .btns{
        width: 100%;
        height: 40px;
        position: absolute;
        bottom: 40px;
        left: 0;
        display: flex;
        justify-content: space-between;
        padding: 0 40px;
        &>div{
          width: 130px;
          height: 40px;
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 14px;
          font-size: 14px;
          font-family: PingFangSC-Semibold, PingFang SC;
          font-weight: 600;
          color: #4677B9;
          border: 1px solid #4677B9;
          border-radius: 2px;
          cursor: pointer;
          &.submit{
            background: #4677B9;
            color: #fff;
            border: none;
          }
        }
      }
    }
  }
  /deep/ .el-input__suffix{
    display: none;
  }
  /deep/ .el-table th.el-table__cell {
    background: #F5F8FA;
    font-size: 15px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #37383D;
  }
  </style>
  